# Установка wireguard для клиента
import os

# ----------------------- [PORTS & INTERFACE] -----------------------
WG_PORT = "58548"

# ----------------------- [ADDRESS] -----------------------
CLIENT_ADDRESS = "10.66.66.2/32"
DNS = "10.66.66.1"

# ----------------------- [SERVER IP & KEYS] -----------------------
SERVER_IP = input("SERVER_IP: ")
PUBLIC_KEY_SERVER = input("PUBLIC_KEY_SERVER: ")
PRIVATE_KEY_CLIENT = input("PRIVATE_KEY_CLIENT: ")
PSK_CLIENT = input("PSK_CLIENT: ")

# ----------------------- [WireGuard] -----------------------
os.system("pacman -Syu && pacman -S wireguard-tools")
# os.system("apt install wireguard")

# Создание конфигурационного файла
with open('/etc/wireguard/wg0-client.conf', 'w') as f:
    # [Interface]
    f.write('[Interface]\n')
    f.write(f"Address = {CLIENT_ADDRESS}\n")
    f.write(f"PrivateKey = {PRIVATE_KEY_CLIENT}\n")
    f.write(f"DNS = {DNS}\n")
    f.write('\n')

    # [Peer]
    f.write('[Peer]\n')
    f.write(f"PublicKey = {PUBLIC_KEY_SERVER}\n")
    f.write(f"Endpoint = {SERVER_IP}:{WG_PORT}\n")
    f.write(f"PresharedKey = {PSK_CLIENT}\n")
    f.write("AllowedIPs = 0.0.0.0/0\n")
    f.write("PersistentKeepalive = 21\n")

os.system("wg-quick up wg0-client")


# ----------------------- [DNS] -----------------------
# Set flag change file any programs
os.system("chattr -i /etc/resolv.conf")
os.system("rm -fv /etc/resolv.conf")
os.system("touch /etc/resolv.conf")


# Set DNS server in resolv.conf
with open('/etc/resolv.conf', 'w') as f:
    f.write(f"nameserver {DNS}")

# Set flag not change file any programs
os.system("chattr +i /etc/resolv.conf")
